﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace DentalSoftware
{
    public partial class DieuTriForm : Form
    {
        private BenhNhanDTO benhnhan;
        public BenhNhanDTO kqtimBN;
        private DieuTriDTO ddt;
        public DateTime ngayDieuTri;        
        private int status;

        public DieuTriForm(int width, int height)
        {
            InitializeComponent();
            this.Height = height;
            this.Width = width;
            status = 0;
        }

        public DieuTriForm(int width, int height, BenhNhanDTO bn)
        {
            InitializeComponent();
            this.Height = height;
            this.Width = width;
            benhnhan = bn;
            status = 1;            
        }      

        private void DieuTriForm_Load(object sender, EventArgs e)
        {
            ShowForm();            
        }

        private void ShowForm()
        {
            dgvDSBenhNhan.AutoGenerateColumns = false;
            dgvDSBenhNhan.DataSource = BenhNhanBUS.SelectBenhNhanByAll();
            DataGridViewComboBoxColumn dv = (DataGridViewComboBoxColumn) dgvChiTietDieuTri.Columns[5];
            dv.DataSource = ChooseBUS.SelectAllDMDichVu();
            dv.ValueMember = "Ma";
            dv.DisplayMember = "Ten";
         
            if (status == 1)
            {
                int i;
                for (i = 0; i < dgvDSBenhNhan.Rows.Count; i++)
                {
                    if (int.Parse(dgvDSBenhNhan.Rows[i].Cells[0].Value.ToString()) == benhnhan.Ma)
                    {
                        break;
                    }
                }
               // dgvDSBenhNhan.ClearSelection();
                dgvDSBenhNhan.FirstDisplayedScrollingRowIndex = i;
                dgvDSBenhNhan.Refresh();
                dgvDSBenhNhan.CurrentCell = dgvDSBenhNhan.Rows[i].Cells[0];
                dgvDSBenhNhan.Rows[i].Selected = true;
                benhnhan = BenhNhanBUS.SelectBenhNhanByMa(int.Parse(dgvDSBenhNhan.CurrentRow.Cells[0].Value.ToString()));                
            }
            else
            {
                if (dgvDSBenhNhan.Rows.Count > 0)
                {
                    benhnhan = BenhNhanBUS.SelectBenhNhanByMa(int.Parse(dgvDSBenhNhan.Rows[0].Cells[0].Value.ToString()));                   
                }
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {            
            ThemDotDieuTri frm = new ThemDotDieuTri(this);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BenhNhanBUS.InsertDotDieuTri(benhnhan.Ma, ngayDieuTri);
                ltbDotDieuTri.DataSource = ChooseBUS.SelectAllDotDieuTriByMaBenhNhan(benhnhan.Ma);
                ltbDotDieuTri.DisplayMember = @"NgayKham";
                ltbDotDieuTri.ValueMember = @"Ma";
            }

        }

        private void dgvDSBenhNhan_SelectionChanged(object sender, EventArgs e)
        {           
            benhnhan = BenhNhanBUS.SelectBenhNhanByMa(int.Parse(dgvDSBenhNhan.CurrentRow.Cells[0].Value.ToString()));
            ltbDotDieuTri.DataSource = ChooseBUS.SelectAllDotDieuTriByMaBenhNhan(benhnhan.Ma);
            ltbDotDieuTri.DisplayMember = @"NgayKham";
            ltbDotDieuTri.ValueMember = @"Ma";

            lblGiaTriMaHS.Text = benhnhan.Ma.ToString();
            lblGiaTriHoTen.Text = benhnhan.Hoten;
            if (benhnhan.DaChupPhim == 1)
                ckbDaChupPhim.Checked = true;
            else
                ckbDaChupPhim.Checked = false;

            lblTienConNo.Text = (benhnhan.ChiPhi - benhnhan.DuaTruoc).ToString();
            if (ltbDotDieuTri.Items.Count == 0)
            {
                txtLyDoKham.Text = "";
                txtChanDoan.Text = "";
                dgvChiTietDieuTri.DataSource = null;
            }
            else
            {
                ltbDotDieuTri.SelectedIndex = 0;                
            }
        }     
        
        public bool IsNumber(string pValue)
        {
            foreach (Char c in pValue)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }        

        private void ltbDotDieuTri_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ltbDotDieuTri.SelectedValue != null && IsNumber(ltbDotDieuTri.SelectedValue.ToString()))
                {
                    int temp = int.Parse(ltbDotDieuTri.SelectedValue.ToString());
                    ddt = BenhNhanBUS.SelectDotDieuTriByMa(temp);
                    txtNgayDieuTri.Text = ltbDotDieuTri.Text;
                    dgvChiTietDieuTri.AutoGenerateColumns = false;
                    dgvChiTietDieuTri.DataSource = BenhNhanBUS.SelectChiTietDieuTriByDotDieuTri(temp);
                }
                if (ddt != null)
                {
                    txtLyDoKham.Text = ddt.LyDoKham;
                    txtChanDoan.Text = ddt.ChanDoan;
                }
                
            }
            catch (FormatException ex)
            {
                throw ex;
            }
        }       
        
        private void txtTim_TextChanged(object sender, EventArgs e)
        {
            dgvDSBenhNhan.AutoGenerateColumns = false;
            dgvDSBenhNhan.DataSource = BenhNhanBUS.SelectBenhNhanByHoTen(txtTim.Text);
        }      

        private void dgvChiTietDieuTri_Leave(object sender, EventArgs e)
        {
            LuuChiTiet(sender,e);
        }

        private void LuuChiTiet(object sender, EventArgs e)
        {
            ddt.LyDoKham = txtLyDoKham.Text;
            ddt.ChanDoan = txtChanDoan.Text;
            BenhNhanBUS.UpdateDotDieuTri(ddt);
            double chiphi, duatruoc;
            chiphi = duatruoc = 0;
            int IsUpdate = 0;
            foreach (DataGridViewRow dr in dgvChiTietDieuTri.Rows)
            {
                if (dr.Cells[0].Value != null)
                {
                    ChiTietDieuTriDTO chitiet = new ChiTietDieuTriDTO();
                    chitiet.MaDotDieuTri = ddt.Ma;
                    int temp;
                   if (int.TryParse(dr.Cells[5].Value.ToString(),out temp) == false)
                       chitiet.MaDichVu = 1;
                   else
                       chitiet.MaDichVu = int.Parse(dr.Cells[5].Value.ToString());
                   
                    chitiet.NgayKham = ddt.NgayKham;
                    chitiet.Rang = dr.Cells[4].Value.ToString();
                    chitiet.CongViec = dr.Cells[6].Value.ToString();
                    chitiet.GhiChu = dr.Cells[10].Value.ToString();
                    if (dr.Cells[0].Value.ToString() != "")
                    {
                        chitiet.Ma = int.Parse(dr.Cells[0].Value.ToString());
                        IsUpdate = 1;
                    }
                    else
                    {
                        chitiet.Ma = dr.Index + 1;
                    }
                    if (dr.Cells[7].Value.ToString() != "")
                    {
                        chitiet.ChiPhi = double.Parse(dr.Cells[7].Value.ToString());
                    }
                    else
                    {
                        chitiet.ChiPhi = 0;
                    }
                    if (dr.Cells[8].Value.ToString() != "")
                    {
                        chitiet.DuaTruoc = double.Parse(dr.Cells[8].Value.ToString());
                    }
                    else
                    {
                        chitiet.DuaTruoc = 0;
                    }
                    if (dr.Cells[9].Value.ToString() != "")
                    {
                        chitiet.ConLai = double.Parse(dr.Cells[9].Value.ToString());
                    }
                    else
                    {
                        chitiet.ConLai = 0;
                    }

                    if (IsUpdate == 1)
                    {
                        ChiTietDieuTriDTO ctdt = BenhNhanBUS.SelectChiTietDieuTriByMa(chitiet.Ma, chitiet.MaDotDieuTri);
                        chiphi += chitiet.ChiPhi - ctdt.ChiPhi;
                        duatruoc += chitiet.DuaTruoc - ctdt.DuaTruoc;
                        IsUpdate = 0;
                    }
                    else
                    {
                        chiphi += chitiet.ChiPhi;
                        duatruoc += chitiet.DuaTruoc;
                    }

                    BenhNhanBUS.InsertUpdateChiTietDieuTri(chitiet);
                }
            }
            benhnhan.DuaTruoc += duatruoc;
            benhnhan.ChiPhi += chiphi;
            BenhNhanBUS.UpdateBenhNhan(benhnhan);
            lblTienConNo.Text = (benhnhan.ChiPhi - benhnhan.DuaTruoc).ToString();
            //ltbDotDieuTri_SelectedIndexChanged(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                BenhNhanBUS.DeleteChiTietDieuTri(ddt.Ma, int.Parse(dgvChiTietDieuTri.CurrentRow.Cells[0].Value.ToString()));
                benhnhan.ChiPhi -= int.Parse(dgvChiTietDieuTri.CurrentRow.Cells[7].Value.ToString());
                benhnhan.DuaTruoc -= int.Parse(dgvChiTietDieuTri.CurrentRow.Cells[8].Value.ToString());
                BenhNhanBUS.UpdateBenhNhan(benhnhan);
                MessageBox.Show("Thành công");                
                dgvChiTietDieuTri.AutoGenerateColumns = false;
                dgvChiTietDieuTri.Rows.RemoveAt(dgvChiTietDieuTri.CurrentRow.Index);
                //dgvChiTietDieuTri.DataSource = BenhNhanBUS.SelectChiTietDieuTriByDotDieuTri(ddt.Ma);
                foreach (DataGridViewRow dr in dgvChiTietDieuTri.Rows)
                {
                    if (dr.Cells[0].Value != null)
                        dr.Cells[0].Value = dr.Index + 1;
                }
                LuuChiTiet(sender, e);
            }
        }

        private void btnXemToanBoChiTiet_Click(object sender, EventArgs e)
        {
            ChiTietDieuTriForm frm = new ChiTietDieuTriForm(benhnhan.Ma);
            frm.ShowDialog();
        }

        private void ckbDaChupPhim_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbDaChupPhim.Checked)
                benhnhan.DaChupPhim = 1;
            else
                benhnhan.DaChupPhim = 0;

            BenhNhanBUS.UpdateBenhNhan(benhnhan);
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            TimBenhNhan frm = new TimBenhNhan(this, 2);
            if (frm.ShowDialog() == DialogResult.Yes)
            {
                dgvDSBenhNhan.AutoGenerateColumns = false;
                dgvDSBenhNhan.DataSource = BenhNhanBUS.SelectBenhNhanByThongTin(kqtimBN);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SoDoRangForm sdr = new SoDoRangForm(benhnhan);
            sdr.ShowDialog();
        }

        private void btnThanhToan_Click(object sender, EventArgs e)
        {
            PhieuThu thu = new PhieuThu(ddt.NgayKham,benhnhan.Hoten,benhnhan.Diachi,benhnhan.Dienthoai,"Thu tiền khám bệnh");
            if (thu.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
            {
                LuuThanhToan(thu.SoTien);
                benhnhan = BenhNhanBUS.SelectBenhNhanByMa(int.Parse(dgvDSBenhNhan.CurrentRow.Cells[0].Value.ToString()));
                lblTienConNo.Text = (benhnhan.ChiPhi - benhnhan.DuaTruoc).ToString();
            }            
        }

        private void LuuThanhToan(double soTien)
        {
            benhnhan.DuaTruoc += soTien;
            BenhNhanBUS.UpdateBenhNhan(benhnhan);
        }

        private void dgvChiTietDieuTri_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            if (e.ColumnIndex == 7 || e.ColumnIndex == 8)
            {
                MessageBox.Show("Dữ liệu bạn vừa nhập vào không hợp lệ. Bạn phải nhập vào một số nguyên, tối thiểu là bằng 0.", 
                    "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
                MessageBox.Show("Dữ liệu bạn vừa nhập vào không hợp lệ. Hãy kiểm tra lại.", 
                    "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void dgvChiTietDieuTri_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 7 || e.ColumnIndex == 8)
            {
                int temp;
                DataGridViewTextBoxCell t = (DataGridViewTextBoxCell)dgvChiTietDieuTri.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (int.TryParse(dgvChiTietDieuTri.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), out temp))
                {
                    //if (int.Parse(dgvChiTietDieuTri.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) < 1000)
                    if (temp < 50000)//((temp % 1000) != 0)
                    {
                        dgvChiTietDieuTri.Rows[e.RowIndex].Cells[e.ColumnIndex].Value =
                            int.Parse(dgvChiTietDieuTri.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) * 1000;
                    }
                }               
            }
        }  
    }
}
